package se.gunneras.invoicesplitter;

public class NumberManipulator {
	private String mPrivatePrefix;
	private String mBrokenPrefix;
	private String mInternationalPrefix;
	private String mIncomingToMobilePrefix;
	
	/**
	 * @param privatePrefix
	 * @param brokenPrefix
	 */
	public NumberManipulator(String privatePrefix, String brokenPrefix, String internationalPrefix, String incomingToMobilePrefix) {
		super();
		mPrivatePrefix = privatePrefix;
		mBrokenPrefix = brokenPrefix;
		mInternationalPrefix = internationalPrefix;
		mIncomingToMobilePrefix = incomingToMobilePrefix;
	}

	public boolean privatePrefixIsBrokenInPhoneNumber(String phoneNumber)
	{
		return phoneNumber.startsWith(mBrokenPrefix);
	}

	public String getFixedPrivatePrefixedPhoneNumber(String phoneNumber)
	{
		if (!phoneNumber.startsWith(mBrokenPrefix))
			throw new IllegalArgumentException("Number does not start with broken prefix.");
		
		return mPrivatePrefix + phoneNumber.substring(mBrokenPrefix.length());
	}
	
	public boolean numberHasPrivatePrefix(String phoneNumber)
	{
		return phoneNumber.startsWith(mPrivatePrefix);
	}
	
	public String addPrivatePrefixToPhoneNumber(String phoneNumber)
	{
		if (phoneNumber.startsWith(mIncomingToMobilePrefix))
			return mPrivatePrefix + phoneNumber.substring(mIncomingToMobilePrefix.length());
		if (phoneNumber.startsWith("+"))
			return mPrivatePrefix + mInternationalPrefix + phoneNumber.substring(1);
		return mPrivatePrefix + phoneNumber;
	}
}
